package com.example.net;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.Html;

import com.example.entity.CircleMember;
import com.example.huashangapp.HSApplication;
import com.example.service.MainService;
import com.example.ui.Userdata;
import com.example.ui.learn.util.CurriculumCenter;
import com.example.ui.learn.util.CurriculumCenter2;
import com.example.ui.learn.util.Detail;
import com.example.ui.learn.util.Listen;
import com.example.ui.learn.util.MyClassroom;
import com.example.ui.learn.util.Recommend;
import com.example.ui.leftmenu.Circle;
import com.example.ui.register.util.City;
import com.example.ui.register.util.Province;
import com.example.ui.sharte.util.SRecommend;

public class AnalyzingJson {
	private static SharedPreferences sp;
	private static Editor spEditor;
	public static ArrayList<Detail> getcontent_detail(String data) {
		ArrayList<Detail> list = new ArrayList<Detail>();
		try {

			JSONArray ja = new JSONArray(data);
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				Detail d = new Detail();
				d.setContent(jo.getString("content"));
				d.setTitle(jo.getString("title"));
				list.add(d);
			}
		} catch (JSONException e) {

			e.printStackTrace();
		}
		return list;
	}

	public static ArrayList<City> getcontent_City(String data) {
		ArrayList<City> list = new ArrayList<City>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				City c = new City();
				c.setCity(jo.getString("city"));
				c.setCityID(jo.getString("cityID"));
				list.add(c);
			}
		} catch (JSONException e) {

			e.printStackTrace();
		}

		return list;
	}

	public static ArrayList<Province> getcontent_province(String data) {
		ArrayList<Province> list = new ArrayList<Province>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				Province p = new Province();
				p.setProvinceID(jo.getString("provinceID"));
				p.setProvince(jo.getString("province"));

				list.add(p);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	public static ArrayList<CircleMember> getCircleMember(String data) {
		ArrayList<CircleMember> list = new ArrayList<CircleMember>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				MainService.circleMember = new CircleMember();
				MainService.circleMember.setId(jo.getString("id"));
				MainService.circleMember.setUserName(jo.getString("username"));
				MainService.circleMember.setHeadPic(jo.getString("headpic2"));
				MainService.circleMember.setZhiwu(jo.getString("zhiwu"));
				MainService.circleMember.setSupply(jo.getString("supply"));
				MainService.circleMember.setRequirement(jo.getString("requirement"));
				list.add(MainService.circleMember);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

	public static ArrayList<Circle> getdata_Circle(String data) {
		ArrayList<Circle> list = new ArrayList<Circle>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				MainService.circle = new Circle();
				MainService.circle.setId(jo.getString("id"));
				MainService.circle.setUid(jo.getString("uid"));
				MainService.circle.setShareNum(jo.getString("shareNum"));
				MainService.circle.setIntro(jo.getString("intro"));
				MainService.circle.setDistance(jo.getString("distance"));
				MainService.circle.setTitle(jo.getString("title"));
				MainService.circle.setJoinNum(jo.getString("joinNum"));
				MainService.circle.setThumb(jo.getString("thumb"));
				MainService.circle.setTypeId(jo.getString("typeId"));
				
				list.add(MainService.circle);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

	public static String return_iaresult(String data) {
		String result = null;

		try {
			JSONObject j = new JSONObject(data);
			result = j.getString("response");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

	public static ArrayList<SRecommend> getcontent_sra(String data) {
		ArrayList<SRecommend> list = new ArrayList<SRecommend>();
		try {
			JSONObject j = new JSONObject(data);
		

			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				SRecommend sr = new SRecommend();
				sr.setContent(jo.getString("content"));
				sr.setTitle(jo.getString("title"));

				list.add(sr);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	public static ArrayList<CurriculumCenter2> getdata_hsa(String data) {
		ArrayList<CurriculumCenter2> list = new ArrayList<CurriculumCenter2>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				CurriculumCenter2 cc2 = new CurriculumCenter2();
				cc2.setId(jo.getString("id"));
				cc2.setLearnnum(jo.getString("playnum"));
				cc2.setThumb(NetRequest.requesthttp + jo.getString("thumb"));
				cc2.setTitle(jo.getString("title"));
				cc2.setTitle1(jo.getString("title1"));
				cc2.setContent1(jo.optString("content1"));
				cc2.setContent2(jo.optString("content2"));
				cc2.setBlId(jo.getString("blId"));
				list.add(cc2);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

	public static String getcontent_result(String data) {
		String result = null;
		try {
			JSONObject j = new JSONObject(data);
			result = j.getString("response");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	public static ArrayList<CurriculumCenter> getdata_item4(String data) {
		ArrayList<CurriculumCenter> list = new ArrayList<CurriculumCenter>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				CurriculumCenter cc = new CurriculumCenter();
				cc.setId(jo.getInt("id"));
				cc.setThumb(NetRequest.requesthttp + jo.optString("thumb"));
				cc.setTitle(jo.getString("title"));
				cc.setTotalCourse(jo.getInt("fangtan"));
				
				list.add(cc);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public static ArrayList<Recommend> getdata_ra(String data) {
		ArrayList<Recommend> list = new ArrayList<Recommend>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				Recommend r = new Recommend();
				r.setId(jo.getString("id"));
				r.setPicid(NetRequest.requesthttp + jo.getString("thumb1"));
				r.setTeacherId(jo.getString("typeId"));
				list.add(r);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

	public static ArrayList<Listen> getdata_la(String data) {
		ArrayList<Listen> list = new ArrayList<Listen>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				Listen l = new Listen();
				l.setId(jo.getString("id"));
				l.setListen(jo.getString("listen"));
				l.setTitle(jo.getString("title"));
				l.setUrl("http://hsxxw.cn"+ jo.optString("url"));
				list.add(l);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

	public static ArrayList<Userdata> getdata_login(String data) {
		sp=HSApplication.sp;
		 spEditor=sp.edit();
		 Userdata userData = new Userdata();
		ArrayList<Userdata> list = new ArrayList<Userdata>();
		try {
			//JSONObject ja = new JSONObject(data);
			JSONArray jo1 = new JSONArray(data);

			JSONObject jo = jo1.getJSONObject(0);
			

			if (jo.has("id")) {
				userData.setUserId(jo.getString("id"));
				spEditor.putString("userid",jo.getString("id"));
			} else {
				userData.setUserId("");
				spEditor.putString("userid","");
			}
			if (jo.has("username")) {
				userData.setUsername(jo.getString("username"));
				spEditor.putString("username",jo.getString("username"));
			} else {
				userData.setUsername("");
				spEditor.putString("username","");
			}
			if(jo.has("pwd")){
				spEditor.putString("pwd",jo.getString("pwd"));
			}else{
				spEditor.putString("pwd","");
			}
			if (jo.has("gender")) {
				userData.setGender(jo.getString("gender"));
				spEditor.putString("gender",jo.getString("gender"));
			} else {
				userData.setGender("");
				spEditor.putString("gender","");
			}
			if (jo.has("mobile")) {
				userData.setMobile(jo.getString("mobile"));
				spEditor.putString("mobile",jo.getString("mobile"));
			} else {
				userData.setMobile("");
				spEditor.putString("mobile","");
			}
			if (jo.has("company")) {
				userData.setCompany(jo.getString("company"));
				spEditor.putString("company",jo.getString("company"));
			} else {
				userData.setCompany("");
				spEditor.putString("company","");
			}
			if (jo.has("zhiwu")) {
				userData.setZhiwu(jo.getString("zhiwu"));
				spEditor.putString("zhiwu",jo.getString("zhiwu"));
			} else {
				userData.setZhiwu("");
				spEditor.putString("zhiwu","");
			}
			if (jo.has("province")) {
				userData.setProvince(jo.getString("province"));
				spEditor.putString("province",jo.getString("province"));
			} else {
				userData.setProvince("");
				spEditor.putString("province","");
			}
			if (jo.has("city")) {
				userData.setCity(jo.getString("city"));
				spEditor.putString("city",jo.getString("city"));
			} else {
				userData.setCity("");
				spEditor.putString("city","");
			}
			if (jo.has("occupation")) {
				userData.setOccupation(jo.getString("occupation"));
				spEditor.putString("occupation",jo.getString("occupation"));
			} else {
				userData.setOccupation("");
				spEditor.putString("occupation","");
			}
			if (jo.has("requirement")) {
				if (!"...".equals(jo.getString("requirement"))) {
					userData.setNeed(jo.getString("requirement"));
					spEditor.putString("requirement",jo.getString("requirement"));
				} else {
					userData.setNeed("");
					spEditor.putString("requirement","");
				}
			} else {
				userData.setNeed("");
				spEditor.putString("requirement","");
			}
			if(jo.has("supply")){
				if (!"...".equals(jo.getString("supply"))) {
					userData.setSupply(jo.getString("supply"));
					spEditor.putString("supply",jo.getString("supply"));
				} else {
					userData.setSupply("");
					spEditor.putString("supply","");
				}
			}else{
				userData.setSupply("");
				spEditor.putString("supply","");
			}
			if(jo.has("headpic1")){
				spEditor.putString("imgHead",Communication.POINT+jo.getString("headpic1"));
			}else{
				spEditor.putString("imgHead","");
			}
			
			if(jo.has("headpic2")){
				spEditor.putString("imgHead2",Communication.POINT+"/"+jo.getString("headpic2"));
			}else{
				spEditor.putString("imgHead2","");
			}
			list.add(userData);
			spEditor.commit();
		} catch (Exception e) {
			userData.setNeed("");
			userData.setSupply("");
			spEditor.commit();
		}

		return list;
	}
	public static ArrayList<MyClassroom> getdata_item2(String data) {
		ArrayList<MyClassroom> list = new ArrayList<MyClassroom>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));

			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				MyClassroom mc = new MyClassroom();
				mc.setId(jo.getString("id"));
				mc.setPic(NetRequest.requesthttp + jo.getString("thumb"));
				mc.setTitle(jo.getString("title"));
				mc.setName(jo.getString("title1"));
				mc.setListen(jo.getString("listen"));
				mc.setTotle(jo.getString("number"));
				mc.setBlId(jo.getString("blId"));
				mc.setIntro("true");
				list.add(mc);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;

	}
	static Runnable item2_run = new Runnable() {
		private String data;
		@Override
		public void run() {
			data = NetRequest.Request_Item02();
			JSONObject j;
			try {
				j = new JSONObject(data);
				String result = j.getString("response");
				if (result.equals("true")) {
					HSApplication.i2_mc= AnalyzingJson.getdata_item2(data);
				} else {
				}
			} catch (JSONException e) {

				e.printStackTrace();
			}
		}
	};
	public static ArrayList<CurriculumCenter> getdata_item1(String data) {
		ArrayList<CurriculumCenter> list = new ArrayList<CurriculumCenter>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				CurriculumCenter cc = new CurriculumCenter();
				cc.setId(jo.getInt("id"));
				cc.setThumb(jo.optString("thumb"));
				cc.setTitle(jo.getString("title"));
				cc.setTotalCourse(jo.optInt("totalCourse"));
				cc.setTotalLecturer(jo.getInt("totalLecturer"));
				list.add(cc);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static ArrayList<CurriculumCenter2> getdata_cca(String data) {
		ArrayList<CurriculumCenter2> list = new ArrayList<CurriculumCenter2>();
		try {
			JSONObject j = new JSONObject(data);
			JSONArray ja = new JSONArray(j.getString("response1"));
			for (int i = 0; i < ja.length(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				CurriculumCenter2 cc2 = new CurriculumCenter2();
				cc2.setId(jo.getString("id"));
				cc2.setLearnnum(jo.getString("learnnum"));
				cc2.setThumb(NetRequest.requesthttp + jo.getString("thumb"));
				cc2.setTitle(jo.getString("title"));
				cc2.setTitle1(jo.getString("title1"));
				cc2.setIntro(jo.getString("intro"));
				cc2.setTypeId(jo.getString("typeId"));
				cc2.setContent1(jo.optString("content1"));
				cc2.setContent2(jo.optString("content2"));
				cc2.setBlId(jo.optString("blId"));
				list.add(cc2);
			}

		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		return list;

	}

}
